package com.ttc.ssm.controller.home;

import com.ttc.ssm.common.SearchJobPage;
import com.ttc.ssm.controller.admin.LoginController;
import com.ttc.ssm.controller.common;
import com.ttc.ssm.po.*;
import com.ttc.ssm.service.admin.CommonService;
import com.ttc.ssm.service.home.HomeCustomerService;
import com.ttc.ssm.service.home.HomeJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Jiarui on 2016/7/21.
 */
@Controller
public class HomeLoginController {

    @Autowired
    HomeCustomerService homeCustomerService;

    @RequestMapping("/loginPage")
    public String loginPage(){
        return "home/login";
    }

    @RequestMapping("/login")
    public String login(HttpSession session, String telephone, String password,String type,Model model) throws Exception {
        System.out.println(type);
        if ((telephone == null ||telephone.equals("")) && type.equals("0")){
            model.addAttribute("msg","请输入手机号！");
            return "forward:/WEB-INF/jsp/home/login.jsp";
        }
        if ((telephone == null ||telephone.equals("")) && type.equals("1")){
            model.addAttribute("msg","请输入注册邮箱！");
            return "forward:/WEB-INF/jsp/home/login.jsp";
        }
        if (password == null || password.equals("")){
            model.addAttribute("msg","请输入密码！");
            return "forward:/WEB-INF/jsp/home/login.jsp";
        }
        if(type.equals("0")){
            if(!common.checkMobileNumber(telephone)){
                model.addAttribute("msg","请输入合法手机号！");
                return "forward:/WEB-INF/jsp/home/login.jsp";
            }
            Customer customer = homeCustomerService.getCustomerByTelephone(telephone);
            if(customer == null){
                model.addAttribute("msg","该用户不存在！");
                return "forward:/WEB-INF/jsp/home/login.jsp";
            }
            if(!customer.getPassword().equals(common.SHA1(customer.getSalt()+common.SHA1(customer.getSalt()+common.SHA1(password))))){
                model.addAttribute("msg","密码错误！");
                return "forward:/WEB-INF/jsp/home/login.jsp";
            }
            if (customer.getStatus()!=1) {
                model.addAttribute("error",1);
                return "forward:/WEB-INF/jsp/home/login.jsp";
            }
            session.setAttribute("fullname", customer.getFullname());
            session.setAttribute("customer_id",customer.getCustomer_id());
            session.setAttribute("type","0");
        }else{
            if(!common.checkEmail(telephone)){
                model.addAttribute("msg","请输入合法邮箱！");
                return "forward:/WEB-INF/jsp/home/login.jsp";
            }
            Company company = homeCustomerService.getCompanyByEmail(telephone);
            if(company == null){
                model.addAttribute("msg","该用户不存在！");
                return "forward:/WEB-INF/jsp/home/login.jsp";
            }
            if(!company.getPassword().equals(common.SHA1(company.getSalt()+common.SHA1(company.getSalt()+common.SHA1(password))))){
                model.addAttribute("msg","密码错误！");
                return "forward:/WEB-INF/jsp/home/login.jsp";
            }
            session.setAttribute("fullname", company.getFullname());
            session.setAttribute("company_id",company.getCompany_id());
            session.setAttribute("type","1");

        }
        String url = (String) session.getAttribute("redirect_url");
        session.removeAttribute("redirect_url");
        System.out.println("url============>"+url);
        if(url != null)
            return "redirect:"+url;
        else
            return "redirect:/index.action";
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) throws Exception{
        request.getSession().invalidate();
        return "redirect:/index.action";
    }

}
